<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Material extends Admin
{

    /**
     * [index]将仓库材料遍历出来
     * @return [type] [description]
     */
    public function index()
    {
        $list = db('material_plan')->select();
          if(empty($list)){
          $this->success('还没有材料详情',url('admin/material/materialPall'));
          }
        return view('admin@material/index', [
            'list' => $list
        ]);

    }
    /**
     * [materialPall description]查询项目内容
     * @return [type] [description]
     */
    public function materialPall()
    {
        $list = db('project_application')->where('status',2)->whereOr('status',1)->select();

        return view('admin@material/contract_name',
            [
                'list'=>$list
            ]);
    }

    /**
     * [materialStorage description]材料入库信息
     * @return [type] [description]
     */
    public function materialStorage()
    {
        $list = db('material_in_warehouse')->order('id desc')->paginate(8);

        $data  = db('project_application')->field(['id','project_name'])->where('status',2)->select();
        $page = $list->render();
// 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page', $page);
        return view('admin@material/materialstorage', [
            $this->fetch(),
            'page'=>$page,
            'list' => $list,
            'data' =>$data
        ]);
    }
    //将材料名遍历到页面
    public function addWarehouse()
    {
        $list = db('material_plan')->select();

        return view('admin@material/addwarehouse', [
            'list' => $list
        ]);

    }
    //将材料入库信息传入数组库。
    public function addDataWarehouse()
    {
        $data = $_POST;
        $a = $data['actual_in_number'];

        $data['in_stock']=$a;

        $result = db('material_in_warehouse')->insertGetId($data);
        if ($result >0) {

            $info['status'] = true;
//            $info['info']= '出库成功';
        }else{
            $info['status'] = $result;
//            $info['info']='出库失败';
        }
        return  json($info);
    }
    /**
     * [materialOutbound description]材料出库信息
     * @return [type] [description]
     */
    public function materialOutbound()
    {
        $list = db('material_out_warehouse')->paginate(8);
//        var_dump($list);
        $page = $list->render();
        return view('admin@material/outmaterial', [
            'page' =>$page,
            'list' => $list
        ]);
    }
    /**
     * [editor description]编辑信息
     * @return [type] [description]
     */
    public function editor($id)
    {
        $list = db('material_plan')->find($id);
        // var_dump($list);exit;
        return view('admin@material/editor',
            ['list'=>$list]
            );
    }
    /**
     * [updateMater description]将更改后数据添加到数据库
     * @return [type] [description]
     */
    public function updateMater($id)
    {
        $data = $_POST;
        $data['estimate_total_price']=($data['estimate_unit_price']) *  ($data['estimate_number']);
        $result = db('material_plan')->where(['id' => $id])->update($data);
        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }

    /**
     * [details description]查看详情内容
     * @return [type] [description]
     */

    public function details($id)
    {
        $list = db('material_plan')->find($id);
//         var_dump($list);

        return view('admin@material/details',
            [
                'list'=>$list
            ]);

        // return 222;
    }
    public function addMaterialForm($id,$project_name)
    {
           $id = $id;
           $project_name=$project_name;
        return view('admin@material/form',
          [
              'id'=>$id,
              'project_name'=>$project_name
          ]);
    }
    //添加项目计划
    public function addMaterial(Request $request)
    {
        $data = input('param.');
        $data['estimate_total_price']=($data['estimate_unit_price']) *  ($data['estimate_number']);

        unset($data['project_application_id']);
        if ($data['project_name'] == null){
            $this->error('请填项目名称!', url('admin/material/materialPall'));
        }
        if ($data['material_name'] == null){
            $this->error('请填写材料名称!',url('admin/material/materialPall'));
        }
        if ($data['timber_division'] == null){
            $this->error('请填写用材分部!',url('admin/material/materialPall'));
        }
        if ($data['estimate_unit_price'] == null){
            $this->error('请填写预估单价!', url('admin/material/materialPall'));
        }
        if ($data['estimate_number'] == null){
            $this->error('请填写预估数量!', url('admin/material/materialPall'));
        }
        if ($data['estimate_total_price'] == null){
            $this->error('请填写预估总价!', url('admin/material/materialPall'));
        }

        $file = $request->file('material_file');

        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/');
            if($info){
                // 成功上传后 获取上传信息
               $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $info->getFilename();
                $data['material_file'] = '/uploads/'.$info->getSaveName();
                $res =explode('\\', $data['material_file']);
                $filesa=implode('/',$res);
                $data['material_file'] =$filesa;

                $reqult = db('material_plan')->insertGetId($data);

                $data = input('param.');
                $betweendata['plan_id'] =$reqult ;
                $betweendata['application_id'] =$data['project_application_id'];
                $Relationship = db('material_plan_application')->insert($betweendata);

                if ($reqult>0){
                    $this->success('材料计划添加成功',url('admin/material/materialPall'));
                }else{
                    $this->error('材料计划添加失败',url('admin/material/materialPall'));
                }

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }



    public function selectProject()
    {
        $list  = db('project_application')->select();
        var_dump($list);
        return json($list);
    }
    //遍历入库材料内容
    public function warehouse($id)
    {
        $list = db('material_in_warehouse')->find($id);
        return view('admin@material/warehouse',
            [
                'list'=>$list
            ]
        );
    }

    public function updateWarehouse($id)
    {
        $data = $_POST;

        $data['in_total_price'] = ($data['actual_unit_price']) * ($data['actual_in_number']);
//        dump($data);
        unset($data['project_name']);
        $result = db('material_in_warehouse')->where(['id' =>$id])->update($data);

        if ($result) {
            $info['status'] = true;
            $info['info'] = '编辑成功';

        }else{
            $info['status'] = false;
            $info['info'] = '操作失败';
        }
        return  json($info);
    }

    public function outWarehouse($id)
    {
        $list = db('material_in_warehouse')->find($id);
//         var_dump($list);exit;
        return view('admin@material/outwarehouse',
            ['list'=>$list]
        );
    }


    public function updateOutWarehouse($id)
    {
        $data = $_POST;
        $result = db('material_in_warehouse')->where(['id' =>$id])->update($data);
        if ($result) {
            $res = db('material_out_warehouse')->insert($data);
            $info['status'] = true;
            $info['info'] = '编辑成功';

        }else{
            $info['status'] = false;
            $info['info'] = '操作失败';

        }
        return  json($info);

    }
    //删除入库的信息
    public function delectOutWarehouse($id)
    {
        $result = db('material_in_warehouse')->where(['id'=>$id])->delete();

        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }


    public function selectMaterial($id)
    {
        $result = db('material_plan_application')->where(['application_id' =>$id])->select();

        $whereplan= Array();

        foreach ($result as $v)
        {
            $whereplan[]= $v['plan_id'];

        }

        foreach ($whereplan as $v)
            {
                $data[] =  db('material_plan')->where('id',$v)->select();
            }
        if(empty($data)){
            $this->success('该项目还没有材料计划',url('admin/material/materialPall'));
        }
        return view('admin@material/contract_index',
            ['list'=>$data]
        );


    }

    public function delecOut($id)
    {
        $result = db('material_out_warehouse')->where(['id' =>$id])->delete();
        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }
    //将一条数据删除

    public function delectMaterial($id)
    {
       $res = db('material_plan_application')->where('plan_id',$id)->delete();
        if($res > 0){
            $list = db('material_plan')->where(['id' =>$id])->delete();
            $info['status']=true;
        }else{
            $info['status']=false;
        }
        return json($info);
    }
}
